<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>cocoOS: docTasks.h File Reference</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">cocoOS
   &#160;<span id="projectnumber">3.0.0</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_tasks_8h.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">docTasks.h File Reference</div>  </div>
</div>
<div class="contents">
<hr/><a name="details" id="details"></a><h2>Detailed Description</h2>
<div class="textblock"><p><b> Tasks </b> <br/>
 The main building block of your application is the task. The tasks should be created in the main function of the application before starting the os. The number of created tasks must not exceed the defined value N_TASKS in <a class="el" href="os__defines_8h.html">os_defines.h</a>. A task is created this way: </p>
<div class="fragment"><pre class="fragment">Msg_t queue[Q_SIZE];

taskId = <a class="code" href="os__appl_a_p_i_8h.html#adb53dc7146d0e27f8ac2675f62dd6124">task_create</a>( taskProc, prio, queue, Q_SIZE, msgSize );
</pre></div><p> The return value, <em>taskId</em>, above is just an uint8_t that can be used to identify the different tasks.<br/>
</p>
<p><em>taskProc</em> is a function pointer to the task procedure that is executed when the task is scheduled to run.<br/>
</p>
<p><em>prio</em> is the task priority in range 1-254, where 1 is highest priority. The kernel always lets the task with highest priority execute first. There can not be two tasks with same priority. <br/>
</p>
<p><em>queue</em> is a pointer to a message queue for the task. If the specified size is 0, no message queue is allocated for the task, and the queue parameter is ignored.<br/>
</p>
<p><em>Q_SIZE</em> is the length of the message queue. Set to 0 if no queue should be used.</p>
<p><em>msgSize</em> is the size of the message type held in the message queue. Ignored of no queue is used.</p>
<p><b>Task Procedures</b><br/>
 The code section of a task procedure must begin and end with the <a class="el" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open()</a> and <a class="el" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close()</a> macros respectively. A very simple task is shown below: </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {
    <a class="code" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open</a>();
    ...
    <a class="code" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close</a>();
}
</pre></div><p> This task will execute only once. When the <a class="el" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close()</a> line is executed, the task is put in the killed state and will not be executed again. If the task has to synchronize with some external event, we can put a blocking call in the middle:</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {
    <a class="code" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open</a>();
    ...
    <a class="code" href="os__appl_a_p_i_8h.html#a576b2326d068ab852846ee612c7b954c">event_wait</a>(evt);
    ...
    <a class="code" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close</a>();
}
</pre></div><p> This task will run to the <a class="el" href="os__appl_a_p_i_8h.html#a576b2326d068ab852846ee612c7b954c">event_wait()</a> call and then block. The kernel will schedule other tasks to execute and eventually the event will be signalled which will put the task back to ready state again. Execution will be resumed after the <a class="el" href="os__appl_a_p_i_8h.html#a576b2326d068ab852846ee612c7b954c">event_wait()</a> call. Once the <a class="el" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close()</a> statement is reached, the task will be killed and will not be executed again.</p>
<p>If we want a task to be excuted periodically we must put the code into an endless loop: </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {
    <a class="code" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open</a>();
    <span class="keywordflow">for</span> (;;) {
        ...
        ...
        <a class="code" href="os__appl_a_p_i_8h.html#ad8232a672a6d4f3a4532bea410a0b1ef">task_wait</a>(TASK_DELAY_TICKS);
    }
    <a class="code" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close</a>();
}
</pre></div><p>Another example of a task waiting for characters to be received on the serial port: </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> task(<span class="keywordtype">void</span>) {
    uint8_t data;
    <a class="code" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open</a>();
    <span class="keywordflow">for</span> (;;) {
        <a class="code" href="os__appl_a_p_i_8h.html#a576b2326d068ab852846ee612c7b954c">event_wait</a>(rxEvt);
        uart_get(&amp;data);
        ...
    }
    <a class="code" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close</a>();
}
</pre></div><p> The rxEvt may be signalled from the uart ISR using <a class="el" href="os__appl_a_p_i_8h.html#a90cd2126d49864ddb72738c12a01b34e">event_ISR_signal(rxEvt)</a>.</p>
<p><b>Task API </b> <br/>
 The task API consists of the following functions and macros:</p>
<ul>
<li><a class="el" href="os__appl_a_p_i_8h.html#adb53dc7146d0e27f8ac2675f62dd6124">task_create()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#a36df952f331631590dc3a982e0e8a2d4">task_kill()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#ac19d6a48c1cb673360ae220aaf2a9d53">task_open()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#a7912af19af4a033fd9caf27879614945">task_close()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#a7d1f6736c118f92f06a5d9218e0ba977">task_suspend()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#a6de09b7f20e384d58bbf330c19447a79">task_resume()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#a5587eb98c16ed4dd2adaa8ae2f3bb1e1">task_state_get()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#ab9e114be01c136878fc271be2a9c45cf">task_id_get()</a></li>
<li><a class="el" href="os__appl_a_p_i_8h.html#ad8232a672a6d4f3a4532bea410a0b1ef">task_wait()</a> </li>
</ul>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="doc_tasks_8h.html">docTasks.h</a>      </li>

    <li class="footer">Generated on Thu Dec 22 2011 21:14:42 for cocoOS by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.6 </li>
   </ul>
 </div>


</body>
</html>
